var sjflag=2;//0单曲,1顺序,2循环,3随机
var timeObj=0;
var curr_total=0;
var lastObj="";
var g_rid="";
var g_name="";
var g_art="";
var p_song_flag=0;
var wb_flag=0;
var cur_index=0;
var lastDt="";
var g_dtId=0;
var $song_Lrc = new Array();
var $song_Lrci = new Array();

function commplay(index){
	tempPlayArr=gdtempPlayArr;
	playsindex(index);
}
function istemplay(flag){
	$(".list").hide();
	$(".dtlist").hide();
	if(1==flag){
		$(".list").show();
	}
}
function playsindex(index){
	normalbg(cur_index);
	jiahongbg(index);//给当前播放的歌曲加颜色
	cur_index=index;
	ptype=1;//临时播放列表
	istemplay(ptype);
	g_dtId=0;
	var mobj=tempPlayArr[index];
	var rid=mobj.rid;
	var name=mobj.songName;
	try{name=mobj.name;}catch(e){}
	var artist = mobj.artist;
	var albumName = mobj.album;
	getSongXml(rid);
}
function getSongXml(rid,flag){
	try{getMedia().play();}catch(e){}
	wb_flag=flag;
	clearload();
	//var ju = './App/Tpl/default/wap/music/htm/play/'+rid+'.js';
	var ju = './App/Tpl/default/wap/music/htm/play/93483.js';
	if(p_song_flag==0){
		p_song_flag=1;
		setTimeout(dsExe,5000);
		g_rid=rid;
		$.ajax({
			type : 'GET',
			url : ju,
			data : {},
			dataType : 'text',
			success : function(data){
                songInfo(data);
            },
			error : function(){
                getridErr();
            }
		});
	}
}
function songInfo(data) {
	p_song_flag=0;
	eval("var sobj="+data);
	if(sobj.ip=="1" || sobj.rid==""){
		clearTimeout(timeObj);
		timeObj=setTimeout(errnext,5000);
		return;
	}
	if(sobj.ip=="0"){
		tk=sobj.mp3path; if(tk.indexOf("://")<0) tk=mp3Url+tk;
		var songName=sobj.songName;
		if(sobj.songName.length>7){
			songName=sobj.songName.substr(0,7)+"...";
		}
		var art=sobj.artist;
		if(sobj.artist>4){
			art=art.substr(0,4)+"...";
		}
		$('#songNameId,.play_title').text(songName);
		$('#songArtId,.play_name').text(art);
		var pic=sobj.artistPic; if(pic.indexOf("://")<0) pic=picUrl+pic;
		$('.foot_pho img').attr("src",pic);
		$('.songer_pho img').attr("src",pic);
		curr_total=sobj.duration;
		$("#totalTimeId").text(getTimeM(sobj.duration));
		$("#totalTimeId2").text(getTimeM(sobj.duration));
		if(wb_flag==1){
			var tempmid=0;
			tempmid=sobj.rid;
			tempPlayArr.push({'rid':tempmid,'songName':songName,'album':sobj.album,'artist':art});
		}
		playSong(tk);
	}
	g_name=sobj.songName;
	g_art=sobj.artist;
	$("#lrctextId")[0].innerHTML='正在加载歌词...';
}
function preSong(flag,autoflag){//0下一首,1上一首
	normalbg(cur_index);//去掉上一首歌曲的颜色
	if(ptype==3){
		if(flag==0){
			if(cur_index<dtList.length-1){
				playdtindex(++cur_index);
			}else{
				if(g_dtId!=0){getdtsj(g_dtId,1);}
			}
		}
	}else{
		if(sjflag==0){//单曲
			if(ptype==1){
				if(autoflag==1){
					playsindex(cur_index);
				}else{
					if(flag==0){
						if(ptype==1){
							if(cur_index<tempPlayArr.length-1){
								playsindex(++cur_index);
							}else{
								playsindex(0);
							}
						}
					}else{
						if(ptype==1){
							if(cur_index>0){
								playsindex(--cur_index);
							}else{
								playsindex(tempPlayArr.length-1);
							}
						}
					}
				}
			}
		}else if(sjflag==1){//顺序
			if(flag==0){
				if(ptype==1){
					if(cur_index<tempPlayArr.length-1){
						playsindex(++cur_index);
					}else{
						if(autoflag!=1)playsindex(0);
					}
				}
			}else{
				if(ptype==1){
					if(cur_index>0){
						playsindex(--cur_index);
					}else{
						playsindex(tempPlayArr.length-1);
					}
				}
			}
		}else if(sjflag==2){//循环
			if(flag==0){
				if(ptype==1){
					if(cur_index<tempPlayArr.length-1){
						playsindex(++cur_index);
					}else{
						playsindex(0);
					}
				}
			}else{
				if(ptype==1){
					if(cur_index>0){
						playsindex(--cur_index);
					}else{
						playsindex(tempPlayArr.length-1);
					}
				}
			}
		}else if(sjflag==3){//随机
			if(ptype==1){
				playsindex(parseInt(tempPlayArr.length*Math.random()))
			}
		}
	}
}
function clearload(){
	try{
		isplay=1;
		getMedia().src="";
		getMedia().load();
	}catch(e){};
}
function getridErr(){
	p_song_flag=0;
	clearTimeout(timeObj);
	timeObj=setTimeout(errnext,5000);
}
function errnext(){
	preSong(0);
}
//播放
function playSong(url){
	//getLrcJson();//加载lrc
	getMedia().src=url;
	try{getMedia().load();}catch(e){};
	getMedia().play();
}
/*播放器状态*/
function getMedia(){
	MediaPlayer.obj = $('#'+MediaPlayer.mediaId)[0];
	return MediaPlayer.obj; 
}
function managerPlst(divId){
	preSong(0,1);
}
function loadStatus(id){
	$("#ctrlBtnId")[0].className='play';
	$("#ctrlBtnId2")[0].className='play';
	setToast("正在加载...");
}
function playStatus(id){
	getLrcJson();
	if(isplay==0){
		setToast("开始播放...");
		if(toastTime!=null){
			clearTimeout(toastTime);
		}
		toastTime=setTimeout(function(){
			$("#toastId").animate({'opacity':'0'},1000);
		},1000);
	}
}
function pauseStatus(id){
	setToast("播放暂停...");
}
function loadError(id){
	//timeObj=setTimeout(errnext,5000);
	if(getMedia().currentSrc!=""){
		//setToast("缓冲失败...");
	}
	$("#"+id).text("加载出错....");
}
var ltime=0;
var isplay=1;
var toastTime=0;
function updateMethod(id){
	if(isplay==1 && Math.floor(getMedia().currentTime)>=1){
		$("#ctrlBtnId")[0].className='stop';
		$("#ctrlBtnId2")[0].className='stop';
		setToast("开始播放...");
		if(toastTime!=null){
			clearTimeout(toastTime);
		}
		toastTime=setTimeout(function(){
			$("#toastId").animate({'opacity':'0'},1000);
		},1000);
		isplay=0;
	}
	var jd=320*getMedia().currentTime/parseFloat(getMedia().duration);
	if(jd>320){
		jd=320;
	}else if(jd<11){
		jd=11;
	}
	
	$(".playing").width(jd);
	$('#currTimeId').text(getTimeM(getMedia().currentTime));
	$('#currTimeId2').text(getTimeM(getMedia().currentTime));
	
	$("#"+id).text(getMedia().duration+","+getMedia().currentTime);
	if(ltime>3){
		moveLrc();
		ltime=0;
	}
	ltime++;
}
//播放器状态结束

//加载lrc歌词
var isloadlrc=0,loadlrcnum=0;
function getLrcJson(){
	if(!$song_Lrc[g_rid]){
		var url="./App/Tpl/default/wap/music/htm/lrc/"+g_rid+".js";
		ku9.jsonp(url,"r="+getDate(2),function(json){log("成功")},function(){log("出错了")});
		isloadlrc=setTimeout("checklrcisload("+g_rid+")", 3000);
	}else{
		processlrcinfo();
	}
}
//检测lrc是否加载成功
function checklrcisload(){
	if(!$song_Lrc[g_rid]){
		loadlrcnum+=1;
		if(loadlrcnum>3){
			loadlrcnum=0;
			clearTimeout(isloadlrc);
			$("#lrctextId")[0].innerHTML="载入歌词失败";
		}else{;
			isloadlrc=setTimeout("getLrcJson()", 3000);
		}
	}else{
		loadlrcnum=0;
		clearTimeout(isloadlrc);
		processlrcinfo();
	}
}
/*lrc字符串处理*/
function processlrcinfo(){
	if($song_Lrc[g_rid].indexOf("文本歌词[/]")!=-1){
		var textword=$song_Lrc[g_rid].split("[/]")[1].replace('　','');
		textword=textword.replace(/\&lt;/g,"<");
		textword=textword.replace(/\&gt;/g,">");
		textword=textword.replace(/\&nbsp;/g," ");
		$("#lrctextId")[0].innerHTML=textword;
		textword="";
	}else if($song_Lrc[g_rid].length >0){
		//处理字符串
		var lrc1=$song_Lrc[g_rid].split("[");
		var array = [];
		for (var i = 0; i < lrc1.length; i++){
			var g = {};
			var t = lrc1[i].split("]");
			g.time = jtime(t[0]);
			if (isNaN(g.time))
				continue;
			g.c = t[1];
			if (g.c == "")
				g.c = getnext(i,lrc1);
			array.push(g);
		}
		array.sort(function(x, y) {
			if (x.time > y.time)
				return 1;
			else if (x.time < y.time)
				return -1;
			else
				return 0;
		});
		$song_Lrci[g_rid]=array;
		//字符串处理结束
		var tin="[";
		var tim="";
		if($song_Lrci[g_rid]){
			var array1=$song_Lrci[g_rid];
			for (var i = 0; i < array1.length; i++){
				var g = array1[i];
				if (!g.c) {
					g.c = "";
				}
				tim+="{'timeId':'"+g.time/1000+"','text':'"+g.c+"'}"
			}
		}
		tim=tim.replace(/(}{)/g,"},{");
		tin+=tim+"]";
		lrcinfo(tin);
		tim="";
		tin="";
	}else{
		$("#lrctextId")[0].innerHTML="暂无歌词";
	}
}
function getnext(y,lrc){
	var result = "";
	var i = y + 1;
	if(lrc[i]){
		t = lrc[i].split("]");
		if (t[1] == "")
			result = getnext(i,lrc);
		else
			result = t[1]
	}
	return result;
}
function jtime(tn){
	var time = 0;
	var ta = tn.split(":");
	if (ta.length < 2)
		return time;

	if (ta[1].indexOf(".") > 0) {
		var tb = ta[1].split(".");
		time = ta[0] * 60 * 1000 + tb[0] * 1000 + tb[1] * 10;
	}
	else
		time = ta[0] * 60 * 1000 + ta[1] * 1000;
	return time;
}
/*lrc字符串处理结束*/

//点击歌词跳到指定时间
function gotime(t){
	if(IsNum(t))
		getMedia().currentTime=t;
}

/*显示格式化的lrc歌词*/
var lrcLst=null;
var llrcObj=null;
function lrcinfo(data){
	eval("lrcLst="+data);
	if(!lrcLst || lrcLst.length==0){
		$("#lrctextId")[0].innerHTML="暂无歌词";
	}else{			
		var htm=[];
		htm[htm.length]="<div id='llrcId' style='overflow-x: hidden;overflow-y: hidden;'>";
		for(var i=0;i<lrcLst.length;i++){
			if(i==0){
				htm[htm.length]='<p id="lId'+i+'" class="lyric_now">';
			}else{
				htm[htm.length]="<p id='lId"+i+"' onclick='gotime("+lrcLst[i].timeId+")'>";	
			}
			htm[htm.length]=lrcLst[i].text;
			htm[htm.length]="</p>";
		}
		htm[htm.length]="</div>";
		$("#lrctextId").html(htm.join(""));
		llrcObj=$("#llrcId");
	}
}
function moveLrc(){
	if(!lrcLst || lrcLst.length==0)return;
	var msec=getMedia().currentTime+1;
	var found=false;
	var mv=0;
	var sIndex=0;
	for (var i = 0; i < lrcLst.length ; i++) {
		if (found == false && msec >= lrcLst[i].timeId && (i == lrcLst.length-1 || lrcLst[i+1].timeId > msec)) {
			mv=i*22;
			sIndex = i;
			found = true;
		}
	}
	if (mv != 0) {
		if($("#lrcId").is(":visible")){
			if(llrcObj != null){
				llrcObj.stop(true,true);
				llrcObj.animate({"margin-top":-mv+"px"});
				if(document.getElementById("llrcId")){
					try{$("#lId"+sIndex)[0].className='lyric_now';}catch(e){}
				}
			}
		}else{
			if(llrcObj != null){
				llrcObj.css({"margin-top":-mv+"px"});
			}
		}
	}
}